const {
  getallessay,
  getessayanyway,
  getessaydetail,
  changcollect,
  addviews,
  search,
  publish,
  getbyuser,
  deleteessaybyid,
  mangetallessay,
  getnotreadycount,
  getallnotice,
  toreaded,
  allchecke,
  changestate,
} = require("../services/essay.services");

class EssayController {
  //获取一定数量的文章
  async getallessay(ctx, next) {
    const { start, size, pass } = ctx.request.query;
    const result = await getallessay(start, size, pass);
    ctx.body = result;
  }
  //以不同的形式获取文章：1:获取最新的文章 2:获取浏览量最高的文章
  async getessayanyway(ctx, next) {
    const { type } = ctx.request.query;
    try {
      const result = await getessayanyway(type);
      ctx.body = result;
    } catch (e) {
      console.log(e);
    }
  }

  //获取文章详情页面
  async getessaydetail(ctx) {
    const { id } = ctx.request.params;
    try {
      const result = await getessaydetail(id);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //收藏文章或者取消收藏
  async changcollect(ctx) {
    const body = ctx.request.body;
    try {
      const result = await changcollect(body);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //增加浏览量
  async addviews(ctx) {
    const { id } = ctx.request.params;
    try {
      const result = await addviews(id);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //根据标题或内容文章
  async search(ctx) {
    const { content } = ctx.request.params;
    try {
      const result = await search(content);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //发表文章
  async publish(ctx) {
    const body = ctx.request.body;
    try {
      const result = await publish(body);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //通过用户id去查询发表的或者收藏的文章
  async getbyuser(ctx) {
    const { id } = ctx.request.params;
    const { type } = ctx.request.query;
    try {
      const result = await getbyuser(id, type);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //删除指定文章
  async deleteessaybyid(ctx) {
    const { id } = ctx.request.params;
    try {
      const result = await deleteessaybyid(id);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //管理员获取所有文章（包含搜索）
  async mangetallessay(ctx) {
    const { start, size, search, starttime, endtime } = ctx.request.query;
    try {
      const result = await mangetallessay(
        start,
        size,
        search,
        starttime,
        endtime
      );
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //根据字段audit查询未读的文章消息的数量
  async getnotreadycount(ctx) {
    const userid = ctx.request.params.userid;
    try {
      const result = await getnotreadycount(userid);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //查询当前登录用户的所有消息
  async getallnotice(ctx) {
    const userid = ctx.request.params.userid;
    try {
      const result = await getallnotice(userid);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //置所有消息为已读
  async toreaded(ctx) {
    const userid = ctx.request.params.userid;
    try {
      const result = await toreaded(userid);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //获取所有需要审核的文章
  async allchecke(ctx) {
    const { start, size, search } = ctx.request.query;
    try {
      const result = await allchecke(start, size, search);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //改变文章审核状态
  async changestate(ctx) {
    const { state } = ctx.request.query;
    const { essayid } = ctx.request.params;
    try {
      const result = await changestate(essayid, state);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }
}

module.exports = new EssayController();
